package com.spring.oaassist.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.oaassist.web.service.ProtalService;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.controller.BaseController;

@Controller
@RequestMapping(value = "/portal")
public class ProtalController extends BaseController{
	
	@Resource ProtalService protalService;
	
	/**
	 * 首页
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/portal/index")
	@RequestMapping(value = "/index")
    public ModelAndView index(HttpServletRequest request,Model model)throws Exception{
        OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
        Map<String, Object> parm = new HashMap<String, Object>();
        parm.put("oaUserId", oaUser.getOa_user_id());
        String[] perArr = {"/portal/rc","/portal/jnr","/portal/khts","/portal/xsyj","/portal/kcyj","/portal/kczc","/portal/rdkh","/portal/xsfy"};
        boolean has_rc = false,has_jnr = false,has_khts = false,has_xsyj = false,has_kcyj = false,has_kczc = false,has_rdkh = false,has_xsfy = false;
        for(String per:perArr){
            boolean ret = ShopOaSessionUtil.checkOaPriv(request,ShopOaSessionUtil.APPPATH+per);
            if (ret&&"/portal/rc".equalsIgnoreCase(per)) {
                has_rc = true;
            }
            if (ret&&"/portal/jnr".equalsIgnoreCase(per)) {
                has_jnr = true;
            }
            if (ret&&"/portal/khts".equalsIgnoreCase(per)) {
                has_khts = true;
            }
            if (ret&&"/portal/xsyj".equalsIgnoreCase(per)) {
                has_xsyj = true;
            }

            if (ret&&"/portal/kcyj".equalsIgnoreCase(per)) {
                has_kcyj = true;
            }
            //库存资产
            if(ret&&"/portal/kczc".equalsIgnoreCase(per)){
                has_kczc = true;
            }
            //热点客户
            if(ret&&"/portal/rdkh".equalsIgnoreCase(per)){
                has_rdkh = true;
            }
            //销售费用
            if(ret&&"/portal/xsfy".equalsIgnoreCase(per)){
                has_xsfy = true;
            }
        }
        if(has_rc){
            //日程
            Map<String, Object> parm1 = new HashMap<String, Object>();
            parm1.put("userId", oaUser.getOa_user_id());
            List<Map<String,Object>> list1 = protalService.queryRcList(parm1);
            model.addAttribute("rc_dataList", list1);
            model.addAttribute("rc_per","1");
        }else {
            model.addAttribute("rc_per","0");
        }
        if(has_jnr){
            //纪念日
            List<Map<String,Object>> list2 = protalService.queryJnrList(null);
            model.addAttribute("jnr_dataList", list2);
            model.addAttribute("jnr_per","1");
        }else {
            model.addAttribute("jnr_per","0");
        }
        if(has_khts){
            //客户投诉
            List<Map<String,Object>> list3 = protalService.queryKhtsList(null);
            model.addAttribute("khts_dataList", list3);
            model.addAttribute("khts_per","1");
        }else {
            model.addAttribute("khts_per","0");
        }
        if(has_xsyj){
            //销售业绩
            Map<String,Object> retMap = protalService.queryXsyjData(parm);
            model.addAttribute("xsyj_dataList", retMap);
            model.addAttribute("xsyj_per","1");
        }else {
            model.addAttribute("xsyj_per","0");
        }
        if(has_kcyj){
            //库存上限预警
            parm.put("kidsParkId", oaUser.getKids_park_id());
            List<Map<String,Object>> kcsxRetMap = protalService.queryKcsxData(parm);
            model.addAttribute("kcsx_dataList", kcsxRetMap);

            //库存下限预警
            parm.put("kidsParkId", oaUser.getKids_park_id());
            List<Map<String,Object>> kcxxRetMap = protalService.queryKcxxData(parm);
            model.addAttribute("kcxx_dataList", kcxxRetMap);
            model.addAttribute("kcyj_per","1");
        }else {
            model.addAttribute("kcyj_per","0");
        }
        if(has_kczc)model.addAttribute("kczc_per","1");else model.addAttribute("kczc_per","0");
        if(has_rdkh)model.addAttribute("rdkh_per","1");else model.addAttribute("rdkh_per","0");
        if(has_xsfy)model.addAttribute("xsfy_per","1");else model.addAttribute("xsfy_per","0");
		return new ModelAndView("main/portal/main_jqueryui");
    }
	
	@RequestMapping(value = "/index2")
    public ModelAndView index2(HttpServletRequest request){
		return new ModelAndView("main/portal/main");
    }
	
	/**
	 * 热点客户
	 * @param pubsrc
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/portal/rdkh")
	@RequestMapping(value = "/rdkh")
    public ModelAndView rdkh(Model model,HttpServletRequest request){
		return new ModelAndView("main/portal/rdkh");
    }
	
	/**
	 * 销售费用
	 * @param pubsrc
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/portal/xsfy")
	@RequestMapping(value = "/xsfy")
    public ModelAndView xsfy(Model model,HttpServletRequest request){
		return new ModelAndView("main/portal/xsfy");
    }
	
	/**
	 * 日程
	 * @param pubsrc
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/portal/rc")
	@RequestMapping(value = "/rc")
    public ModelAndView rc(Model model,HttpServletRequest request){
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		Map<String, Object> parm = new HashMap<String, Object>();
		parm.put("userId", oaUser.getOa_user_id());
        List<Map<String,Object>> list = protalService.queryRcList(parm);
        model.addAttribute("dataList", list);
		return new ModelAndView("main/portal/rc");
    }
	
	/**
	 * 纪念日
	 * @param pubsrc
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/portal/jnr")
	@RequestMapping(value = "/jnr")
    public ModelAndView jnr(Model model,HttpServletRequest request){
		Map<String, Object> parm = new HashMap<String, Object>();
        List<Map<String,Object>> list = protalService.queryJnrList(parm);
        model.addAttribute("dataList", list);
		return new ModelAndView("main/portal/jnr");
    }
	
	/**
	 * 客户投诉
	 * @param pubsrc
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/portal/khts")
	@RequestMapping(value = "/khts")
    public ModelAndView khts(Model model,HttpServletRequest request){
		Map<String, Object> parm = new HashMap<String, Object>();
        List<Map<String,Object>> list = protalService.queryKhtsList(parm);
        model.addAttribute("dataList", list);
		return new ModelAndView("main/portal/khts");
    }
	
	/**
	 * 热点客户
	 * @param page
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryRdkhList")
    @ResponseBody
    public Object queryRdkhList(String page,String rows,HttpServletRequest request){
        Map<String, Object> parm = new HashMap<String, Object>();
        Map<String, Object> retD = new HashMap<String, Object>();
        OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		if(ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
        	parm.put("userId",oaUser.getOa_user_id());
            PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
            List<Map<String,Object>> list = protalService.queryRdkhList(parm);
            PageInfo pageObj = new PageInfo(list);
            retD.put("total", pageObj.getTotal());
            retD.put("rows", list);
            return retD;
		}else{
			return null;
		}
    }
	
	/**
	 * 销售费用
	 * @param page
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryXsfyList")
    @ResponseBody
    public Object queryXsfyList(String page,String rows,HttpServletRequest request){
        Map<String, Object> parm = new HashMap<String, Object>();
        Map<String, Object> retD = new HashMap<String, Object>();
        OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		if(ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
        	parm.put("userId",oaUser.getOa_user_id());
            PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
            List<Map<String,Object>> list = protalService.queryXsfyList(parm);
            PageInfo pageObj = new PageInfo(list);
            retD.put("total", pageObj.getTotal());
            retD.put("rows", list);
            return retD;
		}else{
			return null;
		}
    }
	
	/**
	 * 日程
	 * @param page
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryRcList")
    @ResponseBody
    public Object queryRcList(String page,String rows,HttpServletRequest request){
        Map<String, Object> parm = new HashMap<String, Object>();
        Map<String, Object> retD = new HashMap<String, Object>();
        OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		if(ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
        	parm.put("t_user_id",oaUser.getOa_user_id());
            PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
            List<Map<String,Object>> list = protalService.queryRcList(parm);
            PageInfo pageObj = new PageInfo(list);
            retD.put("total", pageObj.getTotal());
            retD.put("rows", list);
            return retD;
		}else{
			return null;
		}
    }
	
	/**
	 * 纪念日
	 * @param page
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryJnrList")
    @ResponseBody
    public Object queryJnrList(HttpServletRequest request){
        Map<String, Object> parm = new HashMap<String, Object>();
        Map<String, Object> retD = new HashMap<String, Object>();
//        OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
//		if(ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
//        	parm.put("t_user_id",oaUser.getOa_user_id());
            List<Map<String,Object>> list = protalService.queryJnrList(parm);
            retD.put("dataList", list);
            return retD;
//		}else{
//			return null;
//		}
    }
	
	/**
	 * 客户投诉
	 * @param page
	 * @param pageSize
	 * @param request
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryKhtsList")
    @ResponseBody
    public Object queryKhtsList(String page,String rows,HttpServletRequest request){
        Map<String, Object> parm = new HashMap<String, Object>();
        Map<String, Object> retD = new HashMap<String, Object>();
        OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		if(ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
        	parm.put("t_user_id",oaUser.getOa_user_id());
            PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
            List<Map<String,Object>> list = protalService.queryKhtsList(parm);
            PageInfo pageObj = new PageInfo(list);
            retD.put("total", pageObj.getTotal());
            retD.put("rows", list);
            return retD;
		}else{
			return null;
		}
    }
	
	/**
	 * 库存资产
	 * @param page
	 * @param rows
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryKczcList")
    @ResponseBody
    public Object queryKczcList(String page,String rows,HttpServletRequest request){
        Map<String, Object> parm = new HashMap<String, Object>();
        Map<String, Object> retD = new HashMap<String, Object>();
        OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		if(ApplicationUtils.isNotNull(oaUser.getOa_user_id())){
        	parm.put("kidsParkId",oaUser.getKids_park_id());
            List<Map<String,Object>> list = protalService.queryKczcList(parm);
            retD.put("rows", list);
            return retD;
		}else{
			return null;
		}
    }
}
